const post  = async (api, params = {},headers) => {
    return new Promise((reslove, reject) => {
        $.ajax({
            type:"POST",
            url:api,
            dataType:"json",
            data:params,
            beforeSend: function(xhr) {

            },
            headers: headers|| {'Content-Type':'application/json;charset=utf8'},
            success:function(data){
                reslove(data)
            },
            error: function(err){
                reject(err)
            }
        });
    });
}

const get = async (api, params = {},headers) => {
     return new Promise((reslove, reject) => {
         $.ajax({
             type:"GET",
             url:api,
             dataType:"json",
             data:params,
             beforeSend: function(xhr) {

             },
             headers: headers|| {'Content-Type':'application/json;charset=utf8'},
             success:function(data){
                 reslove(data)
             },
             error: function(err){
                 reject(err)
             }
         });
     });
};


//将ajax请求挂到全局对象M上
window.M.request={}
window.M.request.get=get;
window.M.request.post=post;
window.M.request.reqByReq = async (url,req) => {
    if(req.method==="get"){
        let result=  await get(url,req.params,req.headers)
        return result;
    }
    if(req.method==="post"){
        let result= await post(url,req.params,req.headers)
        return result;
    }
};